<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Handlers\ImageUploadHandler;
use Carbon\Carbon;
use App\Models\Goods;



class IndexController extends Controller
{


    public function index(){

        return view('user/index/index');
    }


    /**
     * @param Goods $goods
     * @return 控制台页面
     */
    public function console(Goods $goods){
        // 浏览数据总和
        $data = $goods->requestBrowseTotalNum();
        // 柱状图数据
        list($month , $drawing) = $goods->requestChart();

        return view('user.index.console',compact('data' , 'month' , 'drawing'));
    }


    // 用户信息页面
    public function userInfo(){
        $user = auth()->user();

        return view('user.index.info',compact('user'));
    }


    /**
     * 上传用户头像
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function upImage(Request $request , ImageUploadHandler $upImage)
    {

        $code = 0;
        if ($file = $request->file('file')){
            $path = $upImage->save($file , 'avatars' , auth()->id() , 400);
            $code = 1;
        }
        return response()->json([
            'code'  =>  $code,
            'path'  =>  $path['path'] ?? 0
        ]);
    }


    /**
     * 添加 用户信息数据
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request , UserInfo $userInfo)
    {
        $userInfo->fill($request->all());
        $userInfo->user_id = auth()->id();

        if ($userInfo->save()){
            return response()->json([
                'code'=> 1,
                'msg' => '添加成功',
            ]);
        }else{
            return response()->json([
                'code'=> 0,
                'msg' => '添加失败',
            ]);
        }
    }


    // 修改用户信息数据
    public function update(Request $request , User $user){

        $user->fill($request->all());

        if ($user->update()){
            return response()->json([
                'code'=> 1,
                'msg' => '修改成功',
            ]);
        }else{
            return response()->json([
                'code'=> 0,
                'msg' => '修改失败',
            ]);
        }
    }
    //
}
